package com.add.petback.controller;

import com.add.petback.model.Comment;
import com.add.petback.model.Complainment;
import com.add.petback.model.Pet;
import com.add.petback.model.User;
import com.add.petback.service.impl.CommentServiceImpl;
import com.add.petback.service.impl.ComplainmentServiceImpl;
import com.add.petback.service.impl.PetServiceImpl;
import com.add.petback.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import util.JsonTransformation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/mall")
public class MallController {

    @Autowired(required = false)
    private PetServiceImpl petService;
    @Autowired(required = false)
    private UserServiceImpl userService;
    @Autowired(required = false)
    private ComplainmentServiceImpl complainmentService;
    @Autowired(required = false)
    private CommentServiceImpl commentService;

    @GetMapping("/search")
    @ResponseBody
    //2016112460单富莛
    public String search(@RequestParam String str) {
        Pet pet = new Pet();
        List<Pet> result = new ArrayList<>();
        pet.setName(str);
        List<Pet> foundPet = petService.findTitle(pet);
        for (Pet newpet : foundPet) {
            if (newpet.getStatus().equals("在售"))
                result.add(newpet);
        }
        // 如果查找到的商品为空则返回404
        if (foundPet == null)
            return "404";
        return JsonTransformation.listToJson(result);
    }

    @PostMapping("/complain")
    @ResponseBody
    public String complain(@RequestBody String str) {

        Complainment com = JsonTransformation.jsonToObject(str, Complainment.class);

        try {
            complainmentService.insert(com);
        } catch (Exception e) {
            e.printStackTrace();
            return "404";
        }
        return "200";
    }

    @PostMapping("/comment")
    @ResponseBody
    public String comment(@RequestBody String str) {

        Comment com = JsonTransformation.jsonToObject(str, Comment.class);

        try {
            commentService.insert(com);
        } catch (Exception e) {
            e.printStackTrace();
            return "404";
        }
        return "200";
    }

    @PostMapping("/userupdate")
    @ResponseBody
    public String userupdate(@RequestBody String str) {

        User user = JsonTransformation.jsonToObject(str, User.class);

        try {
            userService.save(user);
        } catch (Exception e) {
            e.printStackTrace();
            return "404";
        }
        return "200";
    }

    @PostMapping("/petupdate")
    @ResponseBody
    public String petupdate(@RequestBody String str) {

        Pet pet = JsonTransformation.jsonToObject(str, Pet.class);

        try {
            petService.save(pet);
        } catch (Exception e) {
            e.printStackTrace();
            return "404";
        }
        return "200";
    }

    @GetMapping("/homepage")
    @ResponseBody
    public String showhome() {
        Pet pet = new Pet();
        List<Pet> result=new ArrayList<>();
        List<Pet> temp = new ArrayList<>();
        List<Pet> foundPet = petService.getAll();
        Collections.reverse(foundPet);
        for (Pet newpet : foundPet) {
            if (newpet.getStatus().equals("在售"))
                temp.add(newpet);
        }
        int count = 0;
        for (Pet newpet : temp) {
            if (count++ >= 2)
                break;
            result.add(newpet);
        }

        return JsonTransformation.listToJson(result);
    }

    @GetMapping("/checkcomments")
    @ResponseBody
    public String checkcomments() {
        Comment com = new Comment();
        List<Comment> result = commentService.getAll();;
        Collections.reverse(result);
        return JsonTransformation.listToJson(result);
    }

    @GetMapping("/mycomments")
    @ResponseBody
    public String mycomments(@RequestParam String str) {
        Comment com = new Comment();
        com.setPhonenumber(str);
        List<Comment> result =commentService.getList(com);
        Collections.reverse(result);
        return JsonTransformation.listToJson(result);
    }

    @PostMapping("/checkorder")
    @ResponseBody
    public String checkorder(@RequestBody String str) {

        Pet pet = JsonTransformation.jsonToObject(str, Pet.class);
        List<Pet> result = new ArrayList<>();
        List<Pet> foundPet = petService.getList(pet);
        // 如果查找到的商品为空则返回404
        if (foundPet == null)
            return "404";
        return JsonTransformation.listToJson(foundPet);
    }

}
